define(function(require, exports, module) {
    'use strict';

    var $ = require('jquery');
    var _ = require('swipe');
    var io = require('request');
    var wx = require('jweixin-1.0.0');

    setTimeout(function() {
        window.scrollTo(0, 1)
    }, 0);

    //生成图片列表
    function makeImgs() {
        var PATH = 'http://s1.bbgstatic.com/m-topic/page/2015/06/zhaopin/images/',
            IMGS = [
                'arrow.png',
                'music-off.png',
                'music-on.png',
                'floor/0/bg.png',
                'floor/0/logo.png',
                'floor/0/yunhou.png',
                'floor/1/bg.png',
                'floor/1/desc.png',
                'floor/1/title.png',
                'floor/2/bg.png',
                'floor/2/desc.png',
                'floor/2/title.png',
                'floor/3/bg.png',
                'floor/3/desc.png',
                'floor/4/bg.png',
                'floor/4/desc.png',
                'floor/5/bg.png',
                'floor/5/desc.png',
                'floor/6/bg.png',
                'floor/6/desc.png',
                'floor/7/bg.png',
                'floor/8/bg.png',
                'floor/9/bg.png',
                'floor/10/bg.png',
                'floor/11/bg1.png',
                'floor/12/bg1.png',
                'floor/13/bg.png'
            ];
        for (var i = 0; i < IMGS.length; i++) {
            IMGS[i] = PATH + IMGS[i]
        }
        return IMGS;
    }

    //图片加载器
    function imgLoading(src, sucFn, errFn) {
        var objImg;
        if (!src) return errFn && errFn();
        if (typeof src === 'string') {
            objImg = new Image();
        } else {
            objImg = src;
        }
        if (sucFn) objImg.onload = function() {
            sucFn(objImg);
        };
        if (sucFn) objImg.onerror = function() {
            errFn(objImg);
        };
        objImg.src = src;
    }

    //转化成百分数,默认保留两位小数
    function toPercent(num, fixed) {
        num = (num * 100).toFixed(fixed || 0);
        return num + '%';
    }

    //页面初始化加载
    function pageLoading(imgs, fn) {
        var totalNum = imgs.length,
            sucNum = 0,
            errNum = 0;
        for (var i = 0; i < totalNum; i++) {
            (function(i) {
                imgLoading(imgs[i], function() {
                    sucNum++;
                    fn && fn(sucNum / totalNum);
                }, function() {
                    errNum++;
                    console.log(imgs[i] + ' failed.');
                    if (sucNum + errNum === totalNum) {
                        fn && fn(1);
                    }
                });
            })(i);
        }
    }

    var $percent = $("#jPercent"),
        $pageBox = $("#jPage"),
        $loadingBox = $("#jLoadingBox"),
        $floorBox = $("#jFloorBox");
    //页面初始化
    pageLoading(makeImgs(), function(percent) {
        $percent.html(toPercent(percent));
        if (percent === 1) {
            $loadingBox.remove();
            floorPage();
            music();
        }
    });

    //音乐播放
    function music() {
        function loadAudio(src, callback) {
            var audio = new Audio(src);
            audio.onloadedmetadata = callback;
            setTimeout(function() {
                audio.src = src;
                audio.play();
            }, 1);
            return audio;
        }

        var audioPath = _CONFIG.audioPath;

        var audio = loadAudio(audioPath, function() {
            //无用
        });

        var $btnMusic = $('#jBtnMusic');

        var options = {
            loop: !0,
            preload: "auto",
            autoplay: true
        };


        for (var i in options) {
            if (options.hasOwnProperty(i) && (i in audio)) {
                audio[i] = options[i];
            }
        }

        var play = function() {
            audio.play();
            $btnMusic.addClass('btn-music-play').data('state', 'playing');
        }

        var pause = function() {
            audio.pause();
            $btnMusic.removeClass('btn-music-play').data('state', 'pauseing');
        }

        // setTimeout(function() {
        //     play();
        // }, 0);
        io.post(url,{},function(rst){
            var data = rst.data || {};
            wx.config({
                debug: false,
                appId: data.appId,
                timestamp: data.timeStamp,
                nonceStr: data.nonceStr,
                signature: data.paySign
            });
            wx.ready(function(){
                play();
            });
        });

        // var touchFlag = true;
        // $('.mod-page').bind('touchstart', function(){
        //     if(touchFlag){
        //         play();
        //         touchFlag = false;
        //     }
        // });

        $btnMusic.css({
            display: 'block'
        }).on('click', function() {
            var state = $btnMusic.data('state');
            if (state === 'playing') {
                pause()
            } else if (!state || state === 'pauseing') {
                play();
            }
        });
    }


    //加载楼层
    function floorPage() {
        var $floors = $floorBox.children('.floor'),
            $arrow = $("#jArrow"),
            len = ($floors && $floors.length) || 0,
            $curFloor,
            $oldFloor,
            curAnimate,
            oldAnimate,
            curIndex = 1,
            oldIndex = curIndex;

        if (len === 0) {
            return;
        }

        var $window = $(window),
            winW = $window.width(),
            winH = $window.height();

        $pageBox.css({
            width: winW,
            height: winH
        });

        $floorBox.css({
            height: winH * len
        });

        $floors.css({
            width: winW,
            height: winH
        });

        var activeDirection, top;
        $floorBox.swipe({
            swipe: function(event, direction, distance, duration, fingerCount) {
                if (direction == "up") {
                    curIndex++;
                } else if (direction == "down") {
                    curIndex--;
                }
                movePage();
            },
            // swipeStatus: function(event, phase, direction, distance, duration, fingerCount) {
            //     top = -(curIndex - 1) * winH;
            //     if (direction == "up") {
            //         activeDirection = -distance;
            //     } else if (direction == "down") {
            //         activeDirection = +distance;
            //     }
            //     if (curIndex > len || curIndex < 1) {
            //         activeDirection = 0;
            //     }
            //     if (phase === 'cancel') {
            //         movePage();
            //     }
            //     if (phase === 'move') {
            //         $floorBox.css({
            //             top: top + activeDirection
            //         });
            //     }
            // },
            threshold: 75
        });
        // $floorBox.swipe({

        // });

        movePage();

        $arrow.show();

        function movePage() {
            if (curIndex > len || curIndex < 1) {
                curIndex = 1;
                $floorBox.css({
                    top: 0
                });
            }

            top = -(curIndex - 1) * winH;
            $curFloor = $floors.eq(curIndex - 1);
            curAnimate = $curFloor.attr('data-animate');
            $oldFloor = $floors.eq(oldIndex - 1);
            oldAnimate = $oldFloor.attr('data-animate');
            $floorBox.animate({
                top: top
            }, function() {
                $oldFloor.removeClass(oldAnimate);
                setTimeout(function() {
                    $curFloor.addClass(curAnimate);
                    oldIndex = curIndex;
                }, 0);
            });
        }
    }
});
